package com.epam.xml.xmlimport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import com.epam.xml.bank.Account;
import com.epam.xml.bank.Card;
import com.epam.xml.bank.Client;

public class SAXParser extends DefaultHandler {

	private List<Client> clients = new ArrayList<Client>();
	private Client currentClient=new Client();
	private Account currentAccount=new Account();
	private Card card=new Card();
	private String currency;
	private int amount;
	private String thisElement;

	private static Logger logger = Logger.getLogger(SAXParser.class);

	@Override
	public void startDocument() throws SAXException {
		logger.info("Parsing XML started!");
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		thisElement = qName;
		if (thisElement.equals("p:client")) {
			currentClient.setLogin(atts.getValue("login"));
			currentClient.setPassword(atts.getValue("password"));
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		switch (thisElement) {
		case "p1:id":
				currentClient.setId(Integer.valueOf(new String(ch, start,
						length)));
			break;
		case "p1:name":
			currentClient.setName(new String(ch, start, length));
			break;
		case "p2:id":
			currentAccount.setId(Integer.valueOf(new String(ch, start,
					length)));
			break;
		case "p3:currency":
			currency=new String(ch,start,length);
			break;
		case "p3:amount":
			amount=Integer.valueOf(new String(ch, start, length));
			break;
		case "p5:cardType":
			card.setCardType(new String(ch, start, length));
			break;
		case "p5:number":
			card.setNumber(new String(ch, start, length));
			break;
		case "p5:pin":
			card.setPin(new String(ch, start, length));
			break;
		}
	}

	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		thisElement = "";
		if(qName=="p1:account") {
			currentAccount.setCard(card);
			HashMap<String,Integer> currencyAndAmount=new HashMap<
					String,Integer>();
			currencyAndAmount.put(currency, amount);
			currentAccount.setCurrencyAndAmount(currencyAndAmount);
			currentClient.addAccount(currentAccount);
			currentAccount=new Account();
			card=new Card();
		}
		if (qName=="p:client") {
			clients.add(currentClient);
			currentClient=new Client();
		}
	}
	
	@Override 
	public void endDocument() { 
	  logger.info(("Parsing XML completed!")); 
	} 
	
	public List<Client> getClients() {
		return clients;
	}
	
	public void warning(SAXParseException exception) {
	    logger.error("WARNING: line " + exception.getLineNumber() + ": "
					+ exception.getMessage());
	}

	public void error(SAXParseException exception) {
	    logger.error("ERROR: line " + exception.getLineNumber() + ": "
					+ exception.getMessage());
	}

	public void fatalError(SAXParseException exception) throws SAXException {
	    logger.error("FATAL: line " + exception.getLineNumber() + ": "
					+ exception.getMessage());
	    throw (exception);
	}

}
